
from django.db import models
from django.db.models.deletion import CASCADE
from django.forms.models import model_to_dict
# Create your models here.
class ShippingContainer(models.Model):
    name=models.TextField(help_text="运柜的名字")
    length=models.FloatField(help_text="运柜的长度")
    width=models.FloatField(help_text="运柜的宽度")
    height=models.FloatField(help_text="运柜的高度") 
    created=models.DateTimeField(auto_now_add=True,help_text="数据创建时间")
from enum import Enum
class TaskStatus(Enum):
    IDLE='idle'
    SUBMITTED='submitted'
    RUNNING='running'
    FINISHED='finished'
    ERROR='error'
    @classmethod
    def choices(cls):
        return tuple(
            (i.value,i.name)
            for i in cls
        )
class Task(models.Model):
    creator = models.ForeignKey(
        'common.User',  # due to circular reference
        related_name='taks',
        on_delete=models.CASCADE,
        null=True, blank=True,
        help_text='创建该任务的用户')
    name=models.TextField(help_text='任务名称')
    status=models.TextField(
        choices=TaskStatus.choices(),
        default=TaskStatus.IDLE.value,
        help_text='任务状态'
    )
    config=models.JSONField(
        help_text="算法参数",
        null=True,
        blank=True
    )
    input_dataset=models.JSONField(
        help_text="算法输入数据",
        null=True,
        blank=True
    )
    result_dataset=models.JSONField(
        help_text="算法结果数据",
        null=True,
        blank=True
    )
    created_at=models.DateTimeField(auto_now_add=True,help_text="任务创建时间")
    submitted_at=models.DateTimeField(help_text="任务计算的提交时间", null=True,blank=True)
    finished_at=models.DateTimeField(help_text="任务计算的结束时间", null=True,blank=True)
    def to_json(self) -> dict:
        return model_to_dict(self)
